package org.btik.context;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 程序上下文，用来存放和获取一些对象
 *
 * @author lustre
 * @since 2022/7/10 09:01
 */

public class SimpleContext implements AppContext {

    private SimpleContext() {
    }

    private static final SimpleContext SIMPLE_CONTEXT = new SimpleContext();

    public static SimpleContext getInstance() {
        return SIMPLE_CONTEXT;
    }

    private final ConcurrentHashMap<String, Object> beanMap = new ConcurrentHashMap<>();


    public <T> T getBean(String beanId) {
        if (null == beanId) {
            return null;
        }
        Object o = beanMap.get(beanId);
        return (T) o;
    }

    @Override
    public <T> List<T> getBeans(Class<T> requiredType) {
        return null;
    }

    @Override
    public <T> T getBean(Class<T> requiredType) {
        return null;
    }

    public static <T> T get(String beanId) {
        return getInstance().getBean(beanId);
    }

    public static void set(String beanId, Object o) {
        getInstance().beanMap.put(beanId, o);
    }

    public static <T> T remove(String beanId){
        return (T) getInstance().beanMap.remove(beanId);
    }


}
